# ARCH  hisiv600 or x86_64
ARCH ?= arm64

CC:=aarch64-target-linux-gnu-gcc
CXX:=aarch64-target-linux-gnu-g++

# Source
SRC_ROOT := $(shell pwd)

# Bulid Info
BUILD_TIME := $(shell date +%Y%m%d)

# App cflags
APP_CFLAGS := -Wall  -pipe -D_LINUX_ -D_GNU_SOURCE 
APP_LIBS := -ldl -lm -lpthread -ldl 

VSS_CFLAGS += $(APP_CFLAGS) -DENABLE_DVPP_INTERFACE

# App Libs
INCLUDE_PATH := -I$(SRC_ROOT)/include
INCLUDE_PATH += -I$(SRC_ROOT)/prebuilts/opencv/$(ARCH)/include/opencv4

APP_LIBS += -L$(SRC_ROOT)/lib
APP_LIBS += -lm -lpthread -lascendcl -lacl_dvpp
# APP_LIBS += -lopencv_imgcodecs -lopencv_imgproc  -lopencv_core -lpthread -lrt -lopencv_objdetect -lopencv_highgui
# APP_LIBS += -lopencv_calib3d -lopencv_flann -lopencv_features2d -lopencv_dnn 

# Path Define
SRC_ROOT	:= $(SRC_ROOT)
OBJ_ROOT	:= $(SRC_ROOT)/.OBJ
EXE_ROOT	:= $(SRC_ROOT)

# Target
NAME := SampleResnetAIPP
LIB := $(EXE_ROOT)/lib/lib$(NAME).so
EXE := $(EXE_ROOT)/$(NAME)

# Source and Object File 
# Cpp Source
SRCS_CPP      := $(shell find $(SRC_ROOT)/src -name '*.cpp')
SRCS_CPP      := $(sort $(SRCS_CPP))

OBJS_CPP      := $(SRCS_CPP:$(SRC_ROOT)%.cpp=$(OBJ_ROOT)/%.o)
OBJS_CPP      := $(sort $(OBJS_CPP))
OBJS_CPP_DIR  := $(dir $(OBJS_CPP))
SRC_CPP_DEPS  := $(OBJS_CPP:%.o=%.d)

# C Source
SRCS_C        := $(shell find $(SRC_ROOT)/src -name '*.c')
SRCS_C        := $(sort $(SRCS_C))

OBJS_C        := $(SRCS_C:$(SRC_ROOT)%.c=$(OBJ_ROOT)/%.o)
OBJS_C        := $(sort $(OBJS_C))
OBJ_C_DIR     := $(dir $(OBJS_C))
SRC_C_DEPS    := $(OBJS_C:%.o=%.d)

CreateDir = $(shell [ -d $1 ] || mkdir -p $1 || echo ":mkdir '$1' fail")

# Create Object and Release Directory
CreateResult :=
dummy := $(call CreateDir, $(OBJ_ROOT))
dummy += $(call CreateDir, $(EXE_ROOT))
dummy += $(foreach dir, $(OBJS_CPP_DIR), CreateResult += $(call CreateDir, $(dir)))
dummy += $(foreach dir, $(OBJ_C_DIR), CreateResult += $(call CreateDir, $(dir)))
ifneq ($(strip CreateResult),)
    err = $(error $(CreateResult))
endif

# Compile Define
COMPILE_CPP    = $(CXX) -fPIC $(PDT_DEFS)  $(VSS_CFLAGS) -c "$<" -o "$@" $(INCLUDE_PATH)
COMPILEDEP_CPP = $(CXX) -MM "$<"  $(PDT_DEFS)  $(VSS_CFLAGS) $(INCLUDE_PATH)
COMPILE_C    = $(CC) -fPIC $(PDT_DEFS) $(VSS_CFLAGS) -c "$<" -o "$@" $(INCLUDE_PATH)
COMPILEDEP_C = $(CC) -MM "$<"  $(PDT_DEFS) $(VSS_CFLAGS) $(INCLUDE_PATH)
LINK = $(CXX) $(APP_CFLAGS) $(APP_LIBS)

.PHONY: all clean distclean
all: prepare target success

prepare:
	@echo -e "\e[0;35;1m [ARCH] $(ARCH)\e[0;36;0m" 

success:
	@echo -e "\e[0;32;1m--compiling '$(notdir $(EXE))' finished\e[0;36;1m" 
	@echo -e "\e[0m" 

$(OBJS_CPP): $(OBJ_ROOT)/%.o :$(SRC_ROOT)/%.cpp
	@echo " [CXX ] " $(notdir $@) " "
	@$(COMPILE_CPP)

$(SRC_CPP_DEPS): $(OBJ_ROOT)/%.d : $(SRC_ROOT)/%.cpp
	@echo " $(COMPILEDEP_CPP) [DEPS] " $(notdir $@)
	@set -e;$(COMPILEDEP_CPP) > $@.$$$$; \
	sed 's,.*\.o[ :]*,$(@:%.d=%.o) $@ : ,g' < $@.$$$$ > $@;rm -f $@.$$$$

$(OBJS_C): $(OBJ_ROOT)/%.o :$(SRC_ROOT)/%.c
	@echo " [CC  ] " $(notdir $@) " "
	@$(COMPILE_C)

$(SRC_C_DEPS): $(OBJ_ROOT)/%.d : $(SRC_ROOT)/%.c
	@echo " [DEPS] " $(notdir $@)
	@set -e;$(COMPILEDEP_C) > $@.$$$$; \
	sed 's,.*\.o[ :]*,$(@:%.d=%.o) $@ : ,g' < $@.$$$$ > $@;rm -f $@.$$$$

target: $(SRC_C_DEPS) $(OBJS_C) $(SRC_CPP_DEPS) $(OBJS_CPP)
	@echo -e "\e[0;34;1m [LINK] $(notdir $(EXE))\e[0;36;0m"
	$(LINK) -o $(EXE) -Wl,--start-group $(OBJS_C) $(OBJS_CPP)  -Wl,--end-group

clean:
	@rm -rf $(EXE)
	@rm -f $(OBJS_CPP)
	@rm -f $(OBJS_C)
	@rm -rf $(OBJ_ROOT)
